from django.db import models


# Create your models here.


class Menu(models.Model):
    """
    菜单表
    """
    title = models.CharField(verbose_name='菜单名称', max_length=32)
    icon = models.CharField(verbose_name='菜单图标', max_length=64)

    def __str__(self):
        return self.title


class Permission(models.Model):
    """
    权限表
    """
    title = models.CharField(verbose_name='权限名称', max_length=32)
    url = models.CharField(verbose_name='带正则的URL', max_length=64)
    name = models.CharField(verbose_name='URL别名', max_length=32, unique=True)
    menu = models.ForeignKey(verbose_name='二级菜单', to='Menu', on_delete=models.CASCADE, null=True, blank=True,
                             help_text='null不可以做菜单， 非null可以做菜单')
    pid = models.ForeignKey(verbose_name='与权限关联的菜单权限', to='Permission', related_name='parents',
                            on_delete=models.CASCADE, null=True, blank=True,
                            help_text='对于非菜单权限，需要选一个可以做菜单的权限做关联，用于默认选择和展开')

    def __str__(self):
        return self.title


class Role(models.Model):
    """
    角色表
    """

    title = models.CharField(verbose_name='角色名称', max_length=32)
    permissions = models.ManyToManyField(verbose_name='权限', to='Permission')

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """
    用户表
    """
    username = models.CharField(verbose_name='用户名', max_length=32, unique=True)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.CharField(verbose_name='邮箱', max_length=128)
    roles = models.ManyToManyField(verbose_name='角色', to=Role, related_name='user_roles')

    def __str__(self):
        return self.username

    class Meta:
        '''
        django在做数据库迁移时不再为UserInfo类创建相应的表和表结构了
        此类可以当成父类，被其他model类继承
        '''
        abstract = True
